// pages/order/index.js
import regeneratorRuntime from '../../lib/runtime/runtime';
import { request } from "../../requests/index.js";
Page({

  
  data: {
    orders:[],

    tabs:[
      {
        id:1,
        title:"全部",
        isActive:true
      },
      {id:2,
        title:"待付款",
        isActive:false
      },
      {id:3,
        title:"待发货",
        isActive:false
      },
      {id:4,
        title:"退款/退货",
        isActive:false
      },
    ]

  },

  handleTabsItemChange(e){
    const {index} = e.detail;
    // let tabs = this.data.tabs;
    // tabs.forEach((v,i)=>v.id===index ? v.isActive=true :v.isActive=false);
    // this.setData({tabs})
    this.changeItemChecked(index);
    this.getOrders(index);
  },

  onShow: function () {
    let token = wx.getStorageSync("token");
    if(!token){
      wx.navigateTo({
        url: '/pages/auth/index'
      });
      return;
    }
    let pages =  getCurrentPages();
    let currentPage = pages[pages.length-1];
    const {type} = currentPage.options;
    // let tab = this.data.tabs;
    // tab.forEach((v,i)=>v.id===type ? v.isActive=true :v.isActive=false);
    // this.setData({tabs:tab})
    this.changeItemChecked(type);
    this.getOrders(type);
  },

  
  async getOrders(type){
    const res = await request({url:"/my/orders/all",data:{type}})
    this.setData({
      orders:res.orders.map(v=>({...v,create_time_cn:(new Date(v.create_time*1000).toLocaleString())}))
    })
  },


  changeItemChecked(index){
    let tabs = this.data.tabs;
    tabs.forEach((v,i)=>v.id===index ? v.isActive=true :v.isActive=false);
    this.setData({tabs})
  },

 
  
})